package com.tvunetworks.center.device.service.impl;

import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.common.util.ResultUtil;
import com.tvunetworks.center.device.service.DeviceService;
import com.tvunetworks.center.device.service.OtherFeignInterfaceService;
import com.tvunetworks.center.device.service.PidPortMappingService;
import com.tvunetworks.center.gs.model.Device;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author Ethan
 * @program:
 * @description:
 * @create: 2021/11/10 13:39
 **/
@Slf4j
@Service
public class OtherFeignServiceImpl implements OtherFeignInterfaceService {


    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private PidPortMappingService pidPortMappingService;
    @Autowired
    private  PidPortMappingServiceImpl pidPortMappingServiceImpl;
    @Override
    public Result<ResponseEntity<String>> getFontMessage(String rid) {

        Device device = deviceService.getCurrentDevice(rid);
        if (device != null) {
            String ip = device.getIp();
            String port = StringUtils.isNotEmpty(device.getPort()) ? String.valueOf(Integer.parseInt(device.getPort()) + 200) : "8288";
            String version = device.getVersion();
            double versionDouble = 0.0;
            if (StringUtils.isNotEmpty(version)) {
                versionDouble = Double.parseDouble(version.length() > 3 ? version.substring(0, 3) : version);
            }
            if (versionDouble >= 7.5) {
                port = pidPortMappingServiceImpl.getNginxPortByPeerId(rid);
            }

            if (StringUtils.isEmpty(port)) {
                log.error("pppppppppppppppppp>port is empty ,peerId:[{}],device:[{}],", rid, device);
                port = "8288";
            }
            String urls = "http://" + ip + ":" + port + "/" + rid + "/captions.txt?0.5653722202841027";

            ResponseEntity<String> responseEntity = null;
            try {
                responseEntity = restTemplate.getForEntity(urls, String.class);
            } catch (Exception e) {
                log.error("getForEntity>>>>>>>>>>>>>", urls, device);
            }

            return ResultUtil.success(responseEntity) ;
        }
        return null;

    }
}
